package
{
	import alternativa.engine3d.core.Mesh;
	import alternativa.engine3d.events.MouseEvent3D;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.utils.MathUtils;
	
	 import flash.events.Event;
     import flash.utils.Timer;
     import alternativa.engine3d.core.Object3D;
	
	public class Portesalle extends Mesh
	{
	
		protected var hauteur:int;
        protected var Larg:int;
        protected var Long:int;
        protected var timer:Timer;
        protected var coeff:Number = 0;
        protected var ouv:Boolean = false;
        private var canOpen :Boolean;
        private var inv:int;
		
		public function Portesalle(inv:int)
		{
			super("Porte");
			this.inv = inv;
			
			canOpen = true;
			
			hauteur = 250;
            Larg = 10;
            Long = 100;
            
	      	createVertex(1,0,0+1,1);
	        createVertex(1,0,hauteur-2,2);
	        createVertex(Long-1,0,0+1,3);
	        createVertex(Long-1,0,hauteur-2,4);    
	        createVertex(Long-1,Larg,0+1,5);
	        createVertex(Long-1,Larg,hauteur-2,6);
	        createVertex(1,Larg,0+1,7);
	        createVertex(1,Larg,hauteur-2,8);
	        

	            
	        createFace([1,2,8,7],"Fl");//left
	        createFace([1,3,4,2],"Ff");//front//
	        createFace([4,3,5,6],"Fr");//right
	        createFace([8,6,5,7],"Fb");//back//
	        createFace([2,4,6,8],"Ft");//top
	        createFace([7,5,3,1],"Fs");//sol
			
			
            if(inv==0){
				createSurface(["Fb"], "Bleu");
				createSurface(["Ff","Fr","Fl"], "Jaune");
			}else{
				createSurface(["Ff"], "Bleu");
				createSurface(["Fb","Fr","Fl"], "Jaune");
			}
			setMaterialToSurface(new FillMaterial(0x0000FF,1,"normal",1,0x0000FF),"Bleu");
			setMaterialToSurface(new FillMaterial(0xFFFF00,1,"normal",1,0xFFFF00),"Jaune");
			
            addEventListener(MouseEvent3D.CLICK,onClick);		
		}
		
		public function onClick(e:MouseEvent3D):void
		{
			if(canOpen) {
				canOpen = false;
				timer= new Timer(0);
				timer.addEventListener("timer", ofPortes);
				timer.start();
			}
		}
		
		public function ofPortes(e:Event):void
		{
			if(ouv == false)
			{
					if(coeff != 90 && coeff != -90)
					{
						    rotationZ = MathUtils.toRadian(coeff);
							if(this.inv == 1)
							coeff ++;
							else
							coeff --;
					}
					else
					{
						timer.stop();
						timer.removeEventListener("timer",ofPortes);
						ouv = true;
						canOpen = true;
					}
			}
			else
			{
				if(this.inv == 0)
				    if(coeff != 1)
					{
							rotationZ = MathUtils.toRadian(coeff);
							coeff++;
					}
					else
					{
						timer.stop();
						timer.removeEventListener("timer",ofPortes);
						coeff = 0;
						ouv = false ;
						canOpen = true;
					}
				else
					if(coeff != -1)
					{
							rotationZ = MathUtils.toRadian(coeff);
							coeff--;
					}
					else
					{
						timer.stop();
						timer.removeEventListener("timer",ofPortes);
						coeff = 0;
						ouv = false ;
						canOpen = true;
					}
			}
		}
	}
}